package com.wangbo.restTemplate.healthcode;

import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

public class TestHealthCode {

    public static final String GET_SKM = "http://58.221.101.74:8090/healthcode/api/getHealthCode";

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplateBuilder().errorHandler(new DefaultResponseErrorHandler(){
            /**
             * 设置RestTemplate自带的异常处理器不做异常处理
             **/
            @Override
            public void handleError(ClientHttpResponse response) {
            }
        }).build();

        test01(restTemplate, "王波","152801199010108556");
    }

    /**
     * 查询苏康码信息
     */
    public static void test01(RestTemplate restTemplate, String name, String personalIdNo) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("name", name);
        param.add("userid", personalIdNo);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        JSONObject jsonObject = restTemplate.postForObject(GET_SKM, httpEntity, JSONObject.class);

        System.out.println(jsonObject.toString());

    }
}
